
goog.provide('esn.EnvironmentSurvey');

goog.require('esn.BaseSurvey');
goog.require('esn.EnvironmentSurveyQuestions');
goog.require('esn.Question');
goog.require('esn.constants');
goog.require('esn.model.QuestionModel');
goog.require('esn.soy.environmentSurvey');
goog.require('goog.array');
goog.require('soy');


/**
 * @constructor
 */
esn.EnvironmentSurvey = function(appContext, opt_domHelper) {
  goog.base(this, appContext, esn.EnvironmentSurvey.SECTION_ID, opt_domHelper);

  this.appContext_ = appContext;

  this.surveyClient_ = appContext.getService(
      esn.constants.ServiceId.SURVEY_CLIENT);
};
goog.inherits(esn.EnvironmentSurvey, esn.BaseSurvey);

esn.EnvironmentSurvey.EventType = {
  BACK: goog.events.getUniqueId('back'),
  CONTINUE: goog.events.getUniqueId('continue')
};

esn.EnvironmentSurvey.SECTION_ID = 'built_env';

esn.EnvironmentSurvey.QUESTION_SECTIONS_ = [
  esn.EnvironmentSurveyQuestions.RESIDENCE_INTERACTIONS,
  esn.EnvironmentSurveyQuestions.CAMPUS_INTERACTIONS
];

esn.PersonalSurvey.prototype.questions_;

esn.PersonalSurvey.prototype.continueButton_;

esn.PersonalSurvey.prototype.backButton_;

esn.EnvironmentSurvey.prototype.createDom = function() {
  goog.base(this, 'createDom');

  var templateData = {
    ResidenceInteractions:
        esn.EnvironmentSurveyQuestions.RESIDENCE_INTERACTIONS,
    CampusInteractions: esn.EnvironmentSurveyQuestions.CAMPUS_INTERACTIONS
  };
  this.setElementInternal(
      soy.renderAsElement(esn.soy.environmentSurvey.content, templateData));

  this.continueButton_ = new goog.ui.CustomButton(
      esn.constants.getContinueButtonCaption());
  this.continueButton_.render(this.getElementByClass('continueButton'));

  this.backButton_ = new goog.ui.CustomButton(
      esn.constants.getBackButtonCaption());
  this.backButton_.render(this.getElementByClass('backButton'));

  this.surveyClient_.getSectionAnswers(esn.EnvironmentSurvey.SECTION_ID,
      goog.bind(this.getSectionAnswersResponse_, this));
};

esn.EnvironmentSurvey.prototype.enterDocument = function() {
  goog.base(this, 'enterDocument');

  this.getHandler().listen(
      this.continueButton_,
      goog.ui.Component.EventType.ACTION,
      this.handleContinueClick_);

  this.getHandler().listen(
      this.backButton_,
      goog.ui.Component.EventType.ACTION,
      this.handleBackClick_);
};

esn.EnvironmentSurvey.prototype.getQuestions = function() {
  return this.questions_ || [];
};

esn.EnvironmentSurvey.prototype.getSectionAnswersResponse_ = function(response) {
  var answerMap = this.createAnswerMap(response);

  this.questions_ = [];
  var questionSectionEls = this.getElementsByClass('questionList');
  // Create question objects for each question section.
  goog.array.forEach(questionSectionEls, function(questionSectionEl, index) {
    var sectionQuestions = this.createSectionQuestions(questionSectionEl,
        esn.EnvironmentSurvey.QUESTION_SECTIONS_[index], answerMap);
    goog.array.extend(this.questions_, sectionQuestions);
  }, this);

  // Update progress bar.
  this.handleQuestionChange();
};

esn.EnvironmentSurvey.prototype.handleContinueClick_ = function(e) {
  if (this.validateQuestions()) {
    this.dispatchEvent(esn.EnvironmentSurvey.EventType.CONTINUE);
  }
};

esn.EnvironmentSurvey.prototype.handleBackClick_ = function(e) {
  this.dispatchEvent(esn.EnvironmentSurvey.EventType.BACK);
};
